<!--
 * @Description: 电子健康档案补传
 * @Autor: 何兴家
 * @Date: 2020-09-25 09:23:10
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-04-13 11:02:59
-->
<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch"></TopFilter>
    <p class="tips">提示：暂只支持切换后的未传工单上传</p>
    <FilterTable
      ref="mainTable"
      columnsRef="repair_electronicArchives_recordSupplementUpload_index"
      :columns="columns"
      :onColumnsChange="columns => (this.columns = columns)"
      :dataSource="tableDataSource"
      :isMemoryPagination="true"
      :onRowSelectChange="handleTableRowSelectChange"
      :defaultSelections="defaultSelectRows"
      :onSyncTableData="syncTableData"
    >
      <template slot="controls">
        <el-button type="primary" icon="el-icon-remove-outline" @click="handleCancelSelectAll">全消</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleSelectAll">全选</el-button>
        <AjaxButton type="primary" icon="el-icon-upload" :click="handleUploadRows">补传档案</AjaxButton>
      </template>
    </FilterTable>
  </div>
</template>

<script>
import { notifyAction } from '@/utils';
import { queryNoUploadRecord, uploadRecord } from '@/api/repair/electronicArchives/recordSupplementUpload';
import { getWxlxSelect } from '@/api/repair/repairReception/customer';

export default {
  name: '',
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      tableDataSource: [], // table数据源
      syncTableDataList: [], // 同步的table列表数据
      defaultSelectRows: [], // 默认选中的行项
      selectedRowData: [] // 选中的table数据
    };
  },
  mounted() {
    this.queryRepairTypeList();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '结算日期',
          fieldName: 'startTime|endTime'
        },
        {
          type: 'SELECT',
          label: '工单类型',
          fieldName: 'repairTypeCode',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          dictItems: []
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '结算日期',
          dataIndex: 'BALANCE_TIME',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '底盘号',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否上传',
          dataIndex: 'UP_HEALTH_TAG',
          sorter: true,
          render: props => {
            return <span>{props.row.UP_HEALTH_TAG == 1 ? '是' : '否'}</span>;
          }
        }
      ];
    },
    // 获取维修类型字典项
    async queryRepairTypeList() {
      let res = await getWxlxSelect();
      if (res.resultCode == 200) {
        let repairTypeDict = res.data.map(x => ({ value: x.REPAIR_TYPE_CODE, text: x.REPAIR_TYPE_NAME }));
        this.topFilterList.find(item => item.fieldName === 'repairTypeCode').itemList = this.columns.find(item => item.dataIndex == 'REPAIR_TYPE_CODE').dictItems = this.columns.find(
          item => item.dataIndex == 'REPAIR_TYPE_CODE'
        ).filterItems = repairTypeDict;
      }
    },
    // 点击查询
    handleSearch(params) {
      this.queryTableData(params);
    },
    // 查询table数据
    async queryTableData(params) {
      this.$refs.mainTable.START_LOADING();
      if (this.tableIsfetching) return notifyAction('数据请求中，请耐心等待', 'info');
      this.tableIsfetching = true;

      try {
        // 格式化时间 去除时分秒
        params.startTime && (params.startTime = params.startTime.split(' ')[0]);
        params.endTime && (params.endTime = params.endTime.split(' ')[0]);

        let res = await queryNoUploadRecord(params);
        if (res.resultCode == 200) {
          this.tableDataSource = res.data;
        }
      } catch (err) {
        console.log(err, '获取table主列表失败');
      } finally {
        this.tableIsfetching = false;
        this.$refs.mainTable.STOP_LOADING();
      }
    },
    // 同步table数据
    syncTableData(list) {
      this.handleCancelSelectAll();
      this.syncTableDataList = list;
    },
    // 选中行项变化
    handleTableRowSelectChange(list) {
      this.selectedRowData = list;
    },
    // 选中全部
    handleSelectAll() {
      this.$refs.mainTable.$refs.pageTable.createRowSelection(this.syncTableDataList);
    },
    // 全消
    handleCancelSelectAll() {
      this.$refs.mainTable.$refs.pageTable.clearSelectionHandle();
    },
    // 点击补传
    async handleUploadRows() {
      if (!this.selectedRowData.length) {
        notifyAction('请选择需要上传的数据', 'warning');
        return false;
      }
      try {
        let res = await uploadRecord({
          roNos: this.selectedRowData.map(item => item.RO_NO).join(',')
        });
        if (res.resultCode == 200 && res.data.status) {
          notifyAction('上传成功', 'success');
          let params = await this.$refs.topFilter.GET_FORM_DATA();
          params[1] && this.queryTableData(params[1]);
        }
      } catch (err) {
        console.log(err, '上传失败');
      }
    }
  }
};
</script>

<style scoped>
.tips {
  font-size: 12px;
  color: red;
  margin: 10px 0;
}
</style>
